ALTER PROCEDURE [dbo].[CPP_User_LuckyWheelRandom]
  @dwUserID INT,    -- 用户ID
  @szResult NVARCHAR(255) OUTPUT  -- 输出信息
AS
BEGIN

	SET NOCOUNT ON

  DECLARE @Channel INT = 0
  SELECT @Channel = Channel FROM RYAccountsDB.dbo.AccountsInfo WHERE AccountsType = 0 AND UserID = @dwUserID
  IF @@ROWCOUNT=0 BEGIN
    SET @szResult = N'玩家[' + CAST(@dwUserID AS NVARCHAR) + ']不存在'
    RETURN 1
  END

  -- 渠道号判断
  DECLARE @FreeCount INT = 0	-- 每日免费次数
  DECLARE @CostCoin INT = 0	-- 单次消耗金币
  SELECT @FreeCount = LuckyWheelFreeCount, @CostCoin = LuckyWheelCostCoin FROM RYPlatformDB.dbo.GameChannel WHERE ID = @Channel
  IF @@ROWCOUNT=0 BEGIN
    SET @szResult = N'渠道[' + CAST(@Channel AS NVARCHAR) + ']不存在'
    RETURN 2
  END

	DECLARE @ItemRateA INT = 0
	DECLARE @ItemCount INT = 0
	SELECT @ItemCount = COUNT(ID), @ItemRateA = SUM(Rate) FROM RYPlatformDB.dbo.LuckyWheelItem WHERE Nullity = 0 AND Channel = @Channel
	
	IF @ItemCount<2 OR @ItemCount>26 BEGIN
    SET @szResult = '渠道[' + CAST(@Channel AS NVARCHAR) + ']转盘奖品数量不在范围[2,26],当前' + CAST(@ItemCount AS NVARCHAR) + '个'
    RETURN 2
	END

	IF @ItemRateA<=0 BEGIN
    SET @szResult = '渠道[' + CAST(@Channel AS NVARCHAR) + ']转盘奖品概率设定异常[' + CAST(@ItemRateA AS NVARCHAR) + ']%'
    RETURN 3
	END

	DECLARE @FreeToday INT = 0	-- 今日免费使用次数
	SELECT @FreeToday = COUNT(ID) FROM RYRecordDB.dbo.RecordLuckyWheelExchange WHERE UserID = @dwUserID AND DATEDIFF(dd, CreateTime, GETDATE()) = 0 AND CostCoin = 0


	(
		-- 随机取
		SELECT
			0 AS Tab, ID AS aa, Title AS bb, Type AS cc, Reward AS dd, ROW_NUMBER () OVER (ORDER BY NEWID()) AS ROW_NUMBER
		FROM RYPlatformDB.dbo.LuckyWheelItem WHERE Nullity = 0 AND Channel = @Channel

	)
	UNION ALL
	(
		SELECT 1 AS Tab, @FreeToday, '', @FreeCount, @CostCoin, 0
	)

	SET @szResult = 'SUCCESS'
	RETURN 0
END